<?php

/**
 * Description of karyawan
 *
 * @author dian
 */
class Karyawan extends Admin_My_Controller {

    var $ARR_BULAN = array(
            "01" => "Januari",
            "02" => "Februari",
            "03" => "Maret",
            "04" => "April",
            "05" => "Mei",
            "06" => "Juni",
            "07" => "Juli",
            "08" => "Agustus",
            "09" => "September",
            "10" => "Oktober",
            "11" => "November",
            "12" => "Desember"
    );

    var $ARR_TAHUN = array();

    public function __construct() {
        parent::__construct();

        $this->load->model('karyawan/model_karyawan');
        $this->load->helper('karyawan');
        $this->load->library('datagrid');
        foreach (range(2010, 2100) as $value) {
            $this->ARR_TAHUN[$value] = $value;
        }
    }

    public function index() {
        $data = array(
                "tahun" => $this->input->get("tahun") ? $this->input->get("tahun", true) : date("Y"),
                "bulan" => $this->input->get("bulan") ? $this->input->get("bulan", true) : date("m"),
                "nama"  => $this->input->get("nama") ? $this->input->get("nama", true) : -1,
                "vessel"  => $this->input->get("vessel") ? $this->input->get("vessel", true) : -1,
                "list_vessel"   => $this->model_karyawan->list_vessel(),
                "list_tahun"    => $this->ARR_TAHUN,
                "list_bulan"    => $this->ARR_BULAN
        );
        if($data["vessel"] != -1) {
            if($data["vessel"] == -2) {
                $whr = "id_group_vessel iS NULL OR id_group_vessel = ''";
            } else {
                $whr = array("id_group_vessel" => $data["vessel"]);
            }
        } else {
            $whr = array("id_group_vessel" => "0");
        }

        $data["list_karyawan"] = $this->model_karyawan->list_karyawan($whr);

        $thn_bln = $data["tahun"]."-".$data["bulan"];

        if($data["nama"] != -1) {
            $absen_av = $this->model_karyawan->check_absen_available($thn_bln, $data["nama"]);

            if(!$absen_av) {
                $jml_hari = date("t", strtotime($thn_bln."-15"));

                generate_absen($thn_bln, $jml_hari, $data["nama"]);
            }
        }

        $result = $this->model_karyawan->list_absensi($thn_bln, $data["nama"]);
        $data["list_lembur"] = $this->model_karyawan->list_lembur();

        $data["numrows"] = $result->num_rows();
        $data["result"] = $result->result();

        $data['date_picker'] = new jDatePicker();
        $data['breadcrumb'] = $this->breadCrumb(true);
        $this->loadMenu('karyawan/index', $data);
    }

    public function list_karyawan() {
        $vessel  = $this->input->post("vessel") ? $this->input->post("vessel", true) : -1;

        if($vessel != -1) {
            if($vessel == -2) {
                $whr = "id_group_vessel IS NULL OR id_group_vessel = ''";
            } else {
                $whr = array("id_group_vessel" => $vessel);
            }
        } else {
            $whr = array("id_group_vessel" => "0");
        }

        $list_karyawan = $this->model_karyawan->list_karyawan($whr);

        foreach($list_karyawan as $key => $value) {
            echo "<option value='$key'>$value</option>";
        }
    }

    public function index_export($tahun, $bulan, $nik) {
        $this->export($this->model_karyawan->get_query_absensi("$tahun-$bulan", $nik), "data_absensi_".$nik."_".$tahun."_".$bulan.".csv");
    }

    public function index_gaji($tahun, $bulan, $nik) {
        $data = array(
                "nik" => $nik,
                "tahun"  => $tahun,
                "bulan"  => $bulan,
                "bln"	 => $this->ARR_BULAN[$bulan],
                "nama"   => $this->model_karyawan->get_scalar("SELECT nama FROM mt_karyawan WHERE nik = '".$nik."'", "nama"),
                "tanggal_masuk"   => $this->model_karyawan->get_scalar("SELECT tanggal_masuk FROM mt_karyawan WHERE nik = '".$nik."'", "tanggal_masuk"),
                "tanggal_keluar"   => $this->model_karyawan->get_scalar("SELECT tanggal_keluar FROM mt_karyawan WHERE nik = '".$nik."'", "tanggal_keluar"),
                "id_divisi" => $this->model_karyawan->get_scalar("SELECT id_divisi FROM mt_karyawan WHERE nik = '".$nik."'", "id_divisi"),
                "id_hutang" => $this->model_karyawan->get_scalar(
                "SELECT d.id_hutang FROM data_hutang d where d.nominal_hutang - (select COALESCE(sum(nominal_dibayarkan),0)
					 from pelunasan_hutang where id_hutang = d.id_hutang) > 0 and d.nik = '".$nik."' 
					 order by d.tanggal_hutang desc limit 1", "id_hutang")
        );

        if($tahun != "" && array_key_exists($bulan, $this->ARR_BULAN)) {
            $gaji_av = $this->model_karyawan->check_gaji_available($tahun, $bulan, $nik);

            if(!$gaji_av) {
                generate_gaji($tahun, $bulan, $nik);
            }
        }

        if($this->input->post("btnSubmit")) {
            $data["result"] = new stdClass();

            $data["result"]->gaji_pokok = $this->input->post("gaji_pokok", true);
            $data["result"]->tunjangan_jabatan = $this->input->post("tunjangan_jabatan", true);
            $data["result"]->prosentase_tunj_anak_1 = $this->input->post("prosentase_tunj_anak_1", true);
            $data["result"]->prosentase_tunj_anak_2 = $this->input->post("prosentase_tunj_anak_2", true);
            $data["result"]->prosentase_tunj_anak_3 = $this->input->post("prosentase_tunj_anak_3", true);            
            $data["result"]->prosentase_tunj_istri_1 = $this->input->post("prosentase_tunj_istri_1", true);            
            $data["result"]->tunjangan_makan = $this->input->post("tunjangan_makan", true);
            $data["result"]->tunjangan_transportasi = $this->input->post("tunjangan_transportasi", true);
            $data["result"]->tunjangan_kehadiran = $this->input->post("tunjangan_kehadiran", true);
            $data["result"]->total_lembur = $this->input->post("total_lembur", true);
            $data["result"]->tunjangan_dinas = $this->input->post("tunjangan_dinas", true);
            $data["result"]->potongan_hutang = $this->input->post("potongan_hutang", true);
            $data["result"]->gaji_diterima = $this->input->post("gaji_diterima", true);

            $is_error = false;

            foreach ((array)$data["result"] as $key => $value) {
                if(!is_numeric($value)) {
                    $data["error"] = "Format ".ucwords(str_replace("_", " ", $key))." tidak valid";
                    $is_error = true;
                    break;
                }
            }

            if(!$is_error) {
                $this->model_karyawan->db->update("data_gaji", (array)$data["result"], array("tahun" => $tahun, "bulan" => $bulan, "nik" => $nik));

                echo "<script>" . CloseGB(true) . "</scr" . "ipt>";
            }
        } else {
            $data["result"] = $this->model_karyawan->data_gaji($tahun, $bulan, $nik);
            $data["jumlah_kehadiran"] = $this->model_karyawan->getJumlahHadir($tahun, $bulan, $nik);
            $data["jumlah_dinas"] = $this->model_karyawan->getJumlahDinas($tahun, $bulan, $nik);
        }

        $this->loadMenuHeaderPopup('karyawan/index_gaji', $data);
    }

    public function index_hutang($nik) {
        echo "Data Hutang $nik";
    }

    public function index_report($tahun, $nik) {
        echo "Rekap Gaji $nik tahun $tahun";
    }

    public function getNamaBulan($index) {
        $bulan=array("JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOVEMBER","DESEMBER");
        return $bulan[$index - 1];
    }

    public function print_gaji() {
        $this->load->library("dcpdf");
        $this->load->model("model_karyawan");
        $data = array();
        $data["gaji_pokok"] = $this->input->get("gaji_pokok");
        $data["jabatan"] = $this->input->get("jabatan");
        $data["anak_1"] = $this->input->get("anak_1");
        $data["anak_2"] = $this->input->get("anak_2");
        $data["anak_3"] = $this->input->get("anak_3");
        $data["istri_1"] = $this->input->get("istri_1");
        $data["makan"] = $this->input->get("makan");
        $data["transportasi"] = $this->input->get("transportasi");
        $data["kehadiran"] = $this->input->get("kehadiran");
        $data["tunjangan_jabatan"] = $this->input->get("jabatan");
        $data["lembur"] = $this->input->get("lembur");
        $data["dinas"] = $this->input->get("dinas");
        $data["hutang"] = $this->input->get("hutang");
        $data["gaji_total"] = $this->input->get("gaji_total");
        $data["bulan"] = $this->getNamaBulan($this->input->get("bulan"));
        $data["tahun"] = $this->input->get("tahun");
        $data["nama"] = $this->model_karyawan->getNamaKaryawan($this->input->get("nik"));
        $data["nik"] = $this->input->get("nik");

        $obj_pdf = new dcpdf('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $obj_pdf->SetCreator(PDF_CREATOR);
        $obj_pdf->setStrukGaji();
        $title = "SLIP GAJI KARYAWAN";
        $data["title"] = $title;
        $obj_pdf->SetTitle($title);
        $obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
        $obj_pdf->setFooterVisible(TRUE);
        $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $obj_pdf->SetDefaultMonospacedFont('helvetica');
        $obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        $obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $obj_pdf->SetFont('times', '', 10);
        $obj_pdf->setFontSubsetting(false);
        $obj_pdf->AddPage();
        $content = $this->load->view("karyawan/print_gaji", $data, true);
        $obj_pdf->writeHTML($content, true, false, true, false, '');
        $obj_pdf->Output('data_gaji.pdf', 'I');
    }

    public function update_absen() {
        $rsp = array(
                "error" => true,
                "msg" => "Tidak dapat terhubung ke server",
                "last" => ""
        );

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $nik = $this->input->post("nik");
            $tgl = $this->input->post("tanggal");
            $ket = $this->input->post("keterangan");

            $rsp["last"] = $this->model_karyawan->get_scalar("SELECT keterangan FROM data_absensi WHERE nik = '$nik' AND tanggal = '$tgl'", "keterangan");
            $result = $this->model_karyawan->update_absen($nik, $tgl, $ket);

            if($result != false) {
                $rsp["error"] = false;
                $rsp["msg"] = "Berhasil";
            } else {
                $rsp["msg"] = "Gagal update absen";
            }
        }

        die(json_encode($rsp));
    }

    public function update_lembur() {
        $rsp = array(
                "error" => true,
                "msg" => "Tidak dapat terhubung ke server"
        );

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $nik = $this->input->post("nik");
            $tgl = $this->input->post("tanggal");
            $tipe = $this->input->post("status");

            $result = $this->model_karyawan->update_lembur($nik, $tgl, $tipe);

            if($result != false) {
                $rsp["error"] = false;
                $rsp["msg"] = "Berhasil";
            } else {
                $rsp["msg"] = "Gagal update absen";
            }
        }

        die(json_encode($rsp));
    }

    public function rekap_absensi(){
        $data = array(
                "tahun" => $this->input->get("tahun") ? $this->input->get("tahun", true) : date("Y"),
                "bulan" => $this->input->get("bulan") ? $this->input->get("bulan", true) : date("m"),
                "list_tahun"    => $this->ARR_TAHUN,
                "list_bulan"    => $this->ARR_BULAN
        );
        if ($this->input->get("tahun")){
            $data["result"] = $this->model_karyawan->get_rekap_absensi($this->input->get("tahun"),$this->input->get("bulan"));
        }
        if ($this->input->get("excel")){
            $data["download"] = true;
            $this->loadMenuNoHeader('karyawan/rekap', $data);
        } else {
           $this->loadMenu('karyawan/rekap', $data);
        }
    }
    
    public function rekap_gaji_abk(){
        $data = array(
                "tahun" => $this->input->get("tahun") ? $this->input->get("tahun", true) : date("Y"),
                "bulan" => $this->input->get("bulan") ? $this->input->get("bulan", true) : date("m"),
                "list_tahun"    => $this->ARR_TAHUN,
                "list_bulan"    => $this->ARR_BULAN
        );
        if ($this->input->get("tahun")){
            $data["result"] = $this->model_karyawan->get_rekap_gaji_abk($this->input->get("tahun"),$this->input->get("bulan"));
        }        
        if ($this->input->get("excel")){            
            $data["download"] = true;
            $this->loadMenuNoHeader('karyawan/gaji_abk', $data);
        } else {
           $this->loadMenu('karyawan/gaji_abk', $data);
        }
    }

}

?>